package com.so206.security;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

/**
 * AES 是一种可逆加密算法，对用户的敏感信息加密处理
 */
public class AESOperator {

    private static final byte[] key1 = {12, 23, 10, 22, 56, 41, 56, 18, 54, 76, 12, 65, 23, 55, 90, 98};
    private static final byte[] key2 = {44, 33, 32, 61, 96, 127, 22, 63, 47, 41, 85, 47, 46, 32, 76, 99};
    private static AESOperator instance = null;

    /**
     * 初始化
     */
    public static AESOperator getInstance() {
        if (instance == null)
            instance = new AESOperator();
        return instance;
    }

    /**
     * 加密
     */
    public String encrypt(String sSrc) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec skeySpec = new SecretKeySpec(key1, "AES");
        IvParameterSpec iv = new IvParameterSpec(key2);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes(StandardCharsets.UTF_8));
        return parseByte2HexStr(encrypted);
    }

    /**
     * 解密
     */
    public String decrypt(String sSrc) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key1, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(key2);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = parseHexStr2Byte(sSrc);
            byte[] original = cipher.doFinal(encrypted1);
            return new String(original, StandardCharsets.UTF_8);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 将byte数组转换成16进制String
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 将16进制String转换为byte数组
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    /**
     * 隐藏内部类
     */
    private class aa {
        private byte a = 12;
        private byte a1 = 12;
        private byte a2 = 12;
        private byte a3 = 12;
        private byte a4 = 12;
        private byte a5 = 12;

    }

    private class abb {
        private byte a3 = 12;
        private byte a = 12;
        private byte a1 = 12;
        private byte a2 = 12;
        private byte a4 = 12;
        private byte a5 = 12;
    }

    private class aacc {
        private byte a3 = 12;
        private byte a = 12;
        private byte a1 = 12;
        private byte a2 = 12;
        private byte a4 = 12;
        private byte a5 = 12;
    }

}